<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class AuthAdmin
{
    public function handle(Request $request, Closure $next)
    {
        if (!Auth::guard('admin')->check()){
            return response()->json(['code'=>-1,'msg'=>'token过期']);
        }
        $admin = Auth::guard('admin')->user();
        $routeName = Route::currentRouteName();
        $adminId = data_get($admin,'id');
        if ($adminId != 1) { //非超级管理员需要判断权限
            if (!Auth::guard('admin')->user()->can($routeName)) {
                return response()->json(['code'=>-1,'msg'=>'没有权限: '.$routeName]);
            }
        }
        global $gameArea;
        $gameArea = (int)$request->input('gameArea',0); //游戏分区号, 默认是0区

        return $next($request);
    }
}
